import { get, post } from '@/utils/request'
import type { User, LoginParams, LoginResult } from '@/types/user'
import type { Result } from '@/types/common'
import {postFormData, put} from "../utils/request.ts";

/**
 * 用户登录
 * @param params 登录参数
 */
export function login(userPhone: string, password: string): Promise<Result<LoginResult>> {
  return post<Result<LoginResult>>('/user/login', { userPhone, password })
}

/**
 * 用户登出
 */
export function logout(): Promise<Result<null>> {
  return post<Result<null>>('/user/logout')
}

/**
 * 获取当前用户信息
 */
export function getUserInfo(): Promise<Result<User>> {
  return get<Result<User>>('/user/info')
}

/**
 * 获取用户列表
 */
export function getUserList(params: any): Promise<Result<User[]>> {
  return get<Result<User[]>>('/user/list', params)
}
export function getUserListByType(params: any): Promise<Result<User[]>> {
  return get<Result<User[]>>('/user/getUserListByType', params)
}

/**
 * 添加用户
 */
export function addUser(data: Partial<User>): Promise<Result<null>> {
  return post<Result<null>>('/user/insert', data)
}

/**
 * 更新用户信息
 */
export function updateUser(data: Partial<User>): Promise<Result<null>> {
  return put<Result<null>>('/user/update', data)
}

/**
 * 删除用户
 */
export function deleteUser(id: number): Promise<Result<null>> {
  return post<Result<null>>('/user/delete', { id })
}

export function updateUserPassword(data: any): Promise<Result<null>> {
  return postFormData<Result<null>>('/user/updatePassword', data)
}
